﻿namespace BEGDC.CC.Web.UserControls.EV
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using BEGDC.CC.BL.CCFlow.EV;
    using BEGDC.CC.Entity;
    using BEGDC.CC.BL.Common;
    using BEGDC.Utility.Castle;
    using BEGDC.CC.BL.Common.Query;
    using BEGDC.CC.BL.CCFlow.CR;
    using BEGDC.CC.BL.Workflow;
    using System.Collections;

    /// <summary>
    ///		Summary description for Summary.
    /// </summary>
    public class Summary : BaseEV
    {
        //protected System.Web.UI.WebControls.CheckBoxList CBL_ASBLeader;
        protected System.Web.UI.WebControls.DataGrid dgASBLeaders;
        protected System.Web.UI.WebControls.Button ibtnClose;
        protected System.Web.UI.WebControls.Button ibtnQuery;
        protected System.Web.UI.WebControls.TextBox lblCluster;
        //protected System.Web.UI.WebControls.Label lblApplicant;
        //protected System.Web.UI.WebControls.Label lblApplicantValue;
        protected System.Web.UI.WebControls.Label lblDept;
        protected System.Web.UI.WebControls.Label lblDeptValue;
        protected System.Web.UI.WebControls.Label lblCATBranch;
        protected System.Web.UI.WebControls.Label lblCATBranchValue;
        protected System.Web.UI.WebControls.Label lblAccount;
        protected System.Web.UI.WebControls.Label lblAccountValue;
        protected System.Web.UI.WebControls.Label lblProposedDate;
        protected System.Web.UI.WebControls.Label lblProposedTime;
        protected System.Web.UI.WebControls.Label lblOptionalDate;
        protected System.Web.UI.WebControls.Label lblCompany;
        protected System.Web.UI.WebControls.Label lblVenue;
        protected System.Web.UI.WebControls.DataGrid dgCustomerList;
        protected System.Web.UI.WebControls.DataGrid dtGridProExecutives;
        protected System.Web.UI.WebControls.TextBox tbOperationFlag;
        protected System.Web.UI.WebControls.TextBox tbOperationContent;
        protected System.Web.UI.HtmlControls.HtmlTable approvedArea;
        protected System.Web.UI.HtmlControls.HtmlTableCell VisitingPurpose;
        protected System.Web.UI.HtmlControls.HtmlTableCell Activies;
        protected System.Web.UI.HtmlControls.HtmlTableCell ProposedExecutives;
        protected System.Web.UI.WebControls.TextBox txtSummary;
        protected System.Web.UI.WebControls.TextBox tbComment;
        protected System.Web.UI.HtmlControls.HtmlTable tbKnowledgeSharing;
        protected System.Web.UI.HtmlControls.HtmlTableRow tr_Title;
        protected System.Web.UI.HtmlControls.HtmlTableCell tdSummary;
        protected System.Web.UI.WebControls.Label Lb_CISWarning;
        protected BEGDC.CC.Web.Common.StampCtrl stampCtrl1;
        protected System.Web.UI.WebControls.Label lbAppNo;
        protected ASB.CC.Web.UserControls.RequesterInfoControl RequesterInfoControl;
        private int CustInfoID
        {
            get
            {
                if (ViewState["CustInfoID"] == null)
                    return 0;
                else
                    return Convert.ToInt32(ViewState["CustInfoID"]);
            }
            set
            {
                ViewState["CustInfoID"] = value;
            }
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!IsPostBack)
            {
                base.InitCommonData();
                InitAppInfo();
                this.RequesterInfoControl.isReadOnly = true;
                //SetStamp();

                BindCustAttendee();
                BindALAttendee();
                BindASBLeaders();
                ButtonStatusSetting();

                //this.ibtnClose.Attributes["onclick"] = "javascript:OnClose()";
                this.ibtnQuery.Attributes.Add("onclick", "javascript:OpenQueryPane(" + Request.QueryString["flowid"] + ")");
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ibtnClose.Click += new EventHandler(ibtnClose_Click);
            this.ibtnQuery.Click += new EventHandler(ibtnQuery_Click);
            this.dgCustomerList.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgCustomerList_ItemDataBound);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void ButtonStatusSetting()
        {
            this.ibtnClose.Visible = Access.IsButtonVisible(EVTransitionList.Close);
            this.ibtnQuery.Visible = Access.IsButtonVisible(EVTransitionList.Query);
        }


        private void InitAppInfo()
        {
            try
            {
                if (this.Application != null && this.Application.AppNo != string.Empty)
                    this.lbAppNo.Text = " Application No:" + this.Application.AppNo;

                #region Init Application Information
                IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
                if (this.Application != null)
                {
                    //this.lblApplicantValue.Text = Application.RequesterNameEN;
                    this.lblDeptValue.Text = Application.RequesterDept;
                    if (Application.DepartmentName != null)
                    {
                        this.lblCATBranchValue.Text = Application.CATBranch;
                        this.lblCluster.Text = Application.Others;
                        //this.lblCluster.CssClass = "Cluster_Block";
                    }
                    this.lblAccountValue.Text = Application.Account;

                    if (Application.DepartmentType.DicItemKey == "CAT")
                    {
                        this.lblAccountValue.Visible = false;
                        this.lblAccount.Visible = false;
                    }
                    if (Application.RequestedDateFrom != Nullables.NullableDateTime.Default)
                    {
                        this.lblProposedDate.Text = WebUtility.FormatDateString(Application.RequestedDateFrom.Value);
                        this.lblProposedTime.Text = Application.RequestedDateFrom.Value.ToString("HH:mm");

                    }
                    if (Application.AlternateDateFrom != Nullables.NullableDateTime.Default)
                    {
                        this.lblOptionalDate.Text = WebUtility.FormatDateString(Application.AlternateDateFrom.Value);
                        //this.lblOptionalTime.Text = Application.AlternateDateFrom.Value.ToString("HH:mm");
                    }
                    this.Activies.InnerHtml = Application.Activity;


                    #region Knowledge Sharing Visibility Setting
                    FlowInstance instance = queryService.QueryFlowInstance(this.Application.Id);

                    if (Access != null && ((EVAccessControl)Access).IsReadOnlyForApp)
                    {
                        this.tr_Title.Visible = false;
                        if (instance.CurrentStep.StepCode != WorkTransition.EV_End)
                        {
                            this.tbKnowledgeSharing.Visible = false;
                        }
                        else
                        {
                            this.txtSummary.Visible = false;
                            this.tdSummary.InnerHtml = Application.Summary;
                        }
                    }

                    #endregion

                }
                #endregion

                #region Init CustomerInfo
                CustomerInfo custInfo = queryService.GetCustomerInfo(this.AppID);

                if (custInfo != null)
                {
                    this.CustInfoID = custInfo.Id;
                    this.lblCompany.Text = custInfo.Company;
                    this.lblVenue.Text = custInfo.Venue;
                    if (custInfo.VisitObjective + string.Empty != string.Empty)
                        this.VisitingPurpose.InnerHtml = custInfo.VisitObjective.Replace("\r\n", "<br>");
                }
                #endregion

                //WebUtility.BuildCheckBoxList(this.CBL_ASBLeader, BEGDC.CC.BL.Common.DicType.ASB_Visiting_Locations);

            }
            catch (BEGDC.Utility.ExceptionManagement.GDCException gdcEx)
            {
                //todo
            }
        }

        private void BindCustAttendee()
        {
            DataTable CustomerDataTable = null;

            if (this.CustInfoID > 0)
            {
                IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
                CustomerDataTable = queryService.GetCustomerAttendeeList(this.CustInfoID);
                if (CustomerDataTable.Select("IsExisted='" + BEGDC.CC.BL.Common.CISFlag.NotExisted + "'").Length > 0)
                {
                    this.Lb_CISWarning.Visible = true;
                }
                else
                {
                    this.Lb_CISWarning.Visible = false;
                }
                this.dgCustomerList.DataSource = CustomerDataTable;
                this.dgCustomerList.DataBind();
            }
        }

        private void BindALAttendee()
        {
            DataTable AccompanyingAttendee = null;

            if (this.AppID > 0)
            {
                ICR crService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                AccompanyingAttendee = crService.GetAccompanyingAttendeeList(this.AppID);
                this.dtGridProExecutives.DataSource = AccompanyingAttendee;
                this.dtGridProExecutives.DataBind();
            }


        }

        void ibtnClose_Click(object sender, EventArgs e)
        {
            try
            {
                IEV evService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.EVService] as IEV;
                this.Application.Summary = this.txtSummary.Text;
                evService.CloseEV(this.Application);
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
            }
            catch
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
            }
        }

        void ibtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.tbOperationFlag.Text == "query")
                {
                    IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
                    string comment = string.Empty;
                    if (Session["Comment"] != null)
                        comment = Session["Comment"].ToString();
                    evService.Query(Convert.ToInt64(Request.QueryString["flowid"]), this.tbOperationContent.Text, comment);
                    Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
                }
            }
            catch
            {
            }
        }

        private void dgCustomerList_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                string IsExisted = e.Item.Cells[6].Text.ToString();
                if (IsExisted == BEGDC.CC.BL.Common.CISFlag.NotExisted)
                {
                    System.Web.UI.WebControls.Image WarningImg = (System.Web.UI.WebControls.Image)e.Item.Cells[7].FindControl("CISWarning");
                    WarningImg.Visible = true;
                    WarningImg.ImageUrl = "../../images/Warning.gif";
                    WarningImg.ToolTip = "The Customer is not in CIS (该客户不是CIS成员)";
                }

                if (IsExisted == BEGDC.CC.BL.Common.CISFlag.Existed)
                {
                    System.Web.UI.WebControls.Image WarningImg = (System.Web.UI.WebControls.Image)e.Item.Cells[7].FindControl("CISWarning");
                    WarningImg.ImageUrl = "../../images/GreenWarningImg.gif";
                    WarningImg.ToolTip = "The Customer is in CIS (该客户是CIS成员)";
                    WarningImg.Visible = true;
                }
            }
        }

        private void BindASBLeaders()
        {
            //this.CBL_ASBLeader.Enabled = false;
            BEGDC.CC.BL.Common.Query.IQuery query = AppContainer.Instance[ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;
            //DataTable table = query.GetAppASBLeadersList(this.Application.Id);
            //this.CBL_ASBLeader.DataSource = table.DefaultView;
            //this.CBL_ASBLeader.DataValueField = "DicItemID";
            //this.CBL_ASBLeader.DataTextField = "DicItemKey";
            //this.CBL_ASBLeader.DataBind();

            DataTable table2 = query.GetAppASBLeadersList2(this.Application.Id);
            this.dgASBLeaders.DataSource = table2;
            this.dgASBLeaders.DataBind();
        }
    }
}
